package com.revenant.driver.ui.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.StringCallback;
import com.lzy.okgo.model.Response;
import com.revenant.driver.R;
import com.revenant.driver.adapter.Driver.DriverOrderItemAdapter;
import com.revenant.driver.base.BaseFragment;
import com.revenant.driver.bean.BaseBean.MessageEvent;
import com.revenant.driver.bean.ShipperBean.OrderListBean;
import com.revenant.driver.ui.activity.OrderCarDetailActivity;
import com.revenant.driver.utils.GsonUtil;
import com.revenant.driver.utils.SPUtils;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadMoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;

import butterknife.BindView;
import butterknife.Unbinder;

import static com.revenant.driver.bean.BaseBean.MyEventCode.Order_Type_SIGAL;
import static com.revenant.driver.utils.Constants.Order_list;

/**
 * @ProjectName: Driver
 * @Package: com.revenant.driver.ui.fragment
 * @ClassName: DriverFragment
 * @Description: java类作用描述
 * @Author: Revenant
 * @CreateDate: 2020-02-07 14:49
 * @UpdateUser: 更新者
 * @UpdateDate: 2020-02-07 14:49
 * @UpdateRemark: 更新说明
 * @Version: 1.0
 */
public class DaiPingJiaFragment extends BaseFragment {
    private DriverOrderItemAdapter mOrderItemAdapter;
    Unbinder unbinder;
    @BindView(R.id.order_recyclerview)
    RecyclerView       orderRecyclerview;
    @BindView(R.id.smartrefresh)
    SmartRefreshLayout smartrefresh;
    private int pagernum = 1;
    @Override
    protected int setLayoutId() {
        return R.layout.fragment_driverorderfour;
    }

    @Override
    protected void initView() {
        mOrderItemAdapter = new DriverOrderItemAdapter();
        orderRecyclerview.setLayoutManager(new LinearLayoutManager(getActivity()));
        orderRecyclerview.setAdapter(mOrderItemAdapter);
        smartrefresh.autoRefresh();

    }

    @Override
    public void initData() {
        smartrefresh.setOnLoadMoreListener(new OnLoadMoreListener() {
            @Override
            public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
                pagernum++;
                initDataRecyclerview("1");
                smartrefresh.finishLoadMore(true);//加载完成
            }
        });
        //刷新
        smartrefresh.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                mOrderItemAdapter.setNewData(null);
                pagernum = 1;
                initDataRecyclerview("1");

                smartrefresh.finishRefresh(true);//刷新完成
            }
        });
        mOrderItemAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                Bundle bundle = new Bundle();
                bundle.putInt("goods_id", mOrderItemAdapter.getData().get(position).getId());
                Intent intent = new Intent();
                intent.putExtras(bundle);
                intent.setClass(getActivity(), OrderCarDetailActivity.class);
                startActivity(intent);
            }
        });

    }

    public static DaiPingJiaFragment newInstance(String param1) {
        DaiPingJiaFragment fragment = new DaiPingJiaFragment();
        Bundle args = new Bundle();
        args.putString(param1, param1);
        fragment.setArguments(args);
        return fragment;
    }
    private void initDataRecyclerview(String isBill) {
        OkGo.<String>get(Order_list)
                .params("driverId", SPUtils.getAccounId(getActivity()))
                .params("status", "3")
                .params("pageNum", pagernum)
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(Response<String> response) {

                        OrderListBean listBean = GsonUtil.parseJsonWithGson(response.body(), OrderListBean.class);
                        int pageNum = listBean.getData().getPageNum();
                        int pages = listBean.getData().getPages();
                        if (listBean.getCode().equals("0")) {
                            if (pageNum == 1) {
                                mOrderItemAdapter.setNewData(listBean.getData().getResult());
                            } else if (pageNum <= pages) {
                                mOrderItemAdapter.addData(listBean.getData().getResult());
                            } else {
                                mOrderItemAdapter.loadMoreEnd();
                            }
//

                        } else {
                            mOrderItemAdapter.loadMoreFail();
                        }

                    }
                });
    }

    @Override
    public void onResume() {
        super.onResume();
    }

    @Override
    protected boolean isRegisterEventBus() {
        return true;
    }

    @Override
    protected void receiveEvent(MessageEvent event) {
        super.receiveEvent(event);
        switch (event.getCode()) {
            case Order_Type_SIGAL:
                smartrefresh.autoRefresh();
                break;
        }
    }
}
